package com.goudan.rabbitmq.demo.ack;

import com.goudan.rabbitmq.demo.util.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * 消息手动应答时，消息不丢失，放回队列重新消费
 */
@Slf4j
public class Producer {
    // 队列名称
    public static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {

        Channel channel = RabbitMqUtils.getChannel();
        // 声明队列：队列名称，是否持久化，是否共享，自动删除，参数
        channel.queueDeclare(TASK_QUEUE_NAME, false, false, false, null);

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String message = scanner.next();
            channel.basicPublish("", TASK_QUEUE_NAME, null,
                    message.getBytes(StandardCharsets.UTF_8));
            log.info("生产者发送消息：{}", message);
        }
    }
}
